// miniprogram/group_chapter/pages/buySourse/buySourse.js
const api=require("../../../utils/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    error:1,
    loading:false,
    month:2,//购买月数
    vid:"",//视频id
    isSelect:0,//是否全买
    module:-1,//课程模块
    orderInfoList:[],
    orderInfo:{},//订单信息
    amount:0,//总金额
    discount:0.6,//折扣
    disAmount:0,//打折后的总金额
   },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.getParams(options);
    this.getData();
  },
  buyCourse:async function () {
    const courseinfo=wx.getStorageSync('COURSEINFO');
    const userdata=wx.getStorageSync('USERDATA');
    const app=getApp();
    const market=app.globalData.market;
    let url;
    let params;
    if(this.data.isSelect){
      // 全买
      url="/order/buyAllNotBuy";
      params={
        'uid':userdata.uid,
        'courseid':courseinfo.id,
        'module':this.data.module,
        'market':market,
        'ExpirationDate':this.data.month,
        'sessionid':userdata.sessionid
      }
    }else{
      //单个购买
      url="/order/buymember"
      params={
        'uid':userdata.uid,
        'market':market,
        'memberSystemid':this.data.orderInfo.memberSystemid,
        'sessionid':userdata.sessionid,
      }
    }
    console.log("%c购买订单","color:red;font-size:15px")
    const res = await api.sendPostsSync(url,params).catch(res=>res);
    if(parseInt(res.errcode)==40051){
        wx.hideLoading()
        this.setData({
          state:false
        })
        wx.showModal({
        title: '提示',
        content: '登录超时，请重新登录~',
        confirmText: "去登录",
        success (res) {
          wx.removeStorageSync("USERINFO");
          wx.removeStorageSync("USERDATA");
          if (res.confirm) {
            wx.reLaunch({
              url: '/pages/my/my',
            })
          }else if (res.cancel) {
            wx.navigateBack({
              delta: 1
            })
          }
        }
      })
      return;
    }
  },
  buyAllCourse:function(){
    this.setData({
      isSelect:!this.data.isSelect
    })
    this.getOrderInfo();
  },
  //获取订单信息列表
  getData:async function(){
    const courseinfo=wx.getStorageSync('COURSEINFO');
    const userdata=wx.getStorageSync('USERDATA');
    const app=getApp();
    const market=app.globalData.market;
    const params={
      'uid':userdata.uid,
      'courseid':courseinfo.id,
      'module':this.data.module,
      'market':market,
      'childId':this.data.vid,
    }
    console.log("%c确认订单","color:red;font-size:15px")
    const res= await api.sendPostsSync("/commodity/getMembersByChildid",params).catch(res=>res)
    if(!parseInt(res.errcode) && res.list.length>0){
      this.setData({
        month:res.list[0].ExpirationDate,
        orderInfoList:res.list,
      })
      this.getOrderInfo();
   }else{
     this.setData({
       error:0
     })
     wx.hideLoading();
   }
  },
  //获取订单信息，通过选择对应的月份
  getOrderInfo:function(){
    const orderInfoList=this.data.orderInfoList
    for(var i=0;i<orderInfoList.length;i++){
      if(orderInfoList[i].ExpirationDate==this.data.month){
        this.setData({
          orderInfo:orderInfoList[i],
          loading:true
        })
      }
    }
    if(this.data.isSelect){
      this.setData({
        amount:(parseFloat(this.data.orderInfo.notBuyCount) * parseFloat(this.data.orderInfo.CostPrice)).toFixed(2),
        disAmount:(parseFloat(this.data.orderInfo.notBuyPrice)).toFixed(2)
      })
    }else{
      this.setData({
        amount:(parseFloat(this.data.orderInfo.CostPrice)).toFixed(2),
        disAmount:(parseFloat(this.data.orderInfo.Price)).toFixed(2)
      })
    }
    wx.hideLoading();
  },
  //获取路径参数
  getParams:function(options){
    const vid=options.vid;
    const module=options.module;
    this.setData({
      vid,module
    })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //选择购买月份
  chooseMonth:function(e){
    const month=e.currentTarget.dataset['index'];
    if(month==this.data.month){
      return ;
    }
    this.setData({
      month
    })
    this.getOrderInfo();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})